﻿CREATE VIEW dbo.v_Test
AS
SELECT     ISNULL(CF_E.ApplID, 0) AS ApplID, S.Name AS Status, CF_E.DPD AS DaysPastDue, CF_E.LoanID, CF_E.[Customer #], CF_E.[Open Field#01], 
                      CONVERT(varchar, CF_E.CompanyID_Raw) + CF_E.[Customer #] AS VantageID, ISNULL(B0.SSN, CF_E.SSN) AS Expr1, CF_E.VIN, CF_E.Serial#, 
                      dbo.t_Loan.StatusID, CF_E.[G_L Date], DATEDIFF(dd, CF_E.[G_L Date], GETDATE()) AS GL_Days, CF_E.[Open Date], 
                      ISNULL(B0.FirstName + ' ' + B0.LastName, CF_E.Name) AS Expr2, ISNULL(B0.Street, CF_E.Address) AS Expr3, ISNULL(B0.CityName, CF_E.City) 
                      AS Expr4, ISNULL(B0.StateCode, CF_E.St) AS Expr5, ISNULL(B0.Zip, CF_E.[Zip Code]) AS Expr6, ISNULL(B0.PhoneHome, CF_E.[Home Phone #]) 
                      AS Expr7, ISNULL(B0.PhoneWork, CF_E.[Work Phone #]) AS Expr8, CASE WHEN [Car Year] > 100 THEN [car year] ELSE [car year] + 1900 END AS Expr9,
                       CF_E.Make, CF_E.Model, CF_E.[Pymnt Schedule], CF_E.[# of Payments], CF_E.[# of PaymentsM], CF_E.[Payment Amount], CF_E.[Total Payments], 
                      CF_E.[1st_ Payment Date], CF_E.FirstPaymentDateCalc, CF_E.[Last Payment Date], CF_E.[Next Payment Date], CF_E.[# Pmts Remaining], 
                      CF_E.[# Pmts RemainingM], CF_E.[Balance Remaining], CF_E.[Total Sales Price], CF_E.[Finance Charges], CF_E.[Status Code], CF_E.P_O_E_, 
                      CF_E.Loan#_Comments, CF_E.[Primary SSN], CF_E.[Amt_ Fin__Disc_ Amt], CF_E.[Interest Income], CF_E.[Earned Discount], CF_E.[Dealer Discount], 
                      CF_E.[Dealer Proceed], CF_E.[Dealer Code], CF_E.[Principal Balance], CF_E.APR, CF_E.Mileage, CF_E.[Collector Id#], CF_E.[Discount%], 
                      CF_E.[Origination State Code], CF_E.Portfolio#, CF_E.[Sales Rep_], CF_E.[Investor Client], CF_E.[Date L_C], CF_E.[Joint Name], CF_E.[Joint Address], 
                      CF_E.[Joint City], CF_E.[Joint State], CF_E.[Joint Zipcode], CF_E.[Joint SSN], CF_E.[Joint H Phone#], CF_E.[Joint W Phone#], ISNULL(B0.PhoneCell, 
                      CF_E.[Primary Cell#]) AS Expr10, CF_E.[Joint Cell#], CF_E.[Partial Payment], CF_E.[NSF Balance], CF_E.[Late Chg Bal], CF_E.[Apply Late Fee], 
                      CF_E.Branch#, CF_E.CompanyID, CF_E.CompanyID_Raw, CF_E.CompanyName, CF_E.[Bankruptcy Filed Y_N?], CF_E.[Bankruptcy Type 07_13?], 
                      CF_E.[Total Principal], CF_E.DPD, CF_E.DPD0, CF_E.DSLP, CF_E.LatePmtsCount * CF_E.[Payment Amount] AS PastDuePayments, 
                      CF_E.LatePmtsCount * CF_E.[Payment Amount] - CASE WHEN CF_E.LatePmtsCount > 0 THEN ISNULL(CF_E.[Partial Payment], 0) 
                      ELSE 0 END + ISNULL(CF_E.LateCharges, 0) + ISNULL(CF_E.[NSF Balance], 0) + ISNULL(CF_E.[Late Chg Bal], 0) AS PastDueAmount, 
                      CF_E.PaymentsMade, CF_E.PaymentsMadeM, CF_E.PaymentsExpectedM, CASE WHEN [status code] IN ('O', 'L') 
                      THEN CF_E.PaymentsExpectedM - CF_E.PaymentsMadeM ELSE 0 END AS PaymentsBehindM, CF_E.DownPayment, CF_E.[Princ/ChOff Ballance], 
                      CF_E.[Repo Date], CF_E.[A_R Balance], CF_E.[Resale Amt_], CF_E.Chargeoff, CF_E.[Final Repo Date], CF_E.Retainage, CF_E.[Other Chgs Bal], 
                      CF_E.UsixID, ISNULL(CF_E.[Title Received Y_N?], 'N') AS [Title Received Y_N?], CF_E.NDay, N_L.DateDue AS [Due Date], DATEDIFF(dd, 
                      CASE WHEN N_L.DateDue > CF_E.[Next Payment Date] THEN N_L.DateDue ELSE ISNULL(CF_E.[Next Payment Date], N_L.DateDue) END, GETDATE()) 
                      AS DueDateSince, DATEPART(hh, GETDATE()) - ISNULL(DATEPART(hh, N_L.DateDue), 9) + ISNULL(dbo.ts_TimeZone.Offset, 0) + 5 AS DueAfterHour, 
                      N_L.DateDueBy, N_L.NoteUser, dbo.ts_TimeZone.TimeZone, dbo.ts_TimeZone.Offset AS TimeZoneOffset, ISNULL(N_PTP_O.DaysToPTP, 999) 
                      AS DaysToPTP, ISNULL(N_PTP_O.DaysToPTP, 999) AS DaysToPTP_Sort, N_PTP_O.DaysToPTP AS DaysToPTP_Real, 
                      N_PTP_O.DateDueBy AS PTP_Agent, CF_E.DPDCalc, N_L.StatusDate AS NoteStatusDate, N_L.NoteDate, CF_E.[Simple_Add-On], CF_E.VINCheck, 
                      CF_E.LateCharges, CF_E.LatePmtsCount, CF_E.RemBalance78, ISNULL(CF_E.[Total Principal], 0) + ISNULL(CF_E.DSLP * CF_E.PerDiem, 0) 
                      + ISNULL(CF_E.LateCharges, 0) + ISNULL(CF_E.[NSF Balance], 0) AS PayOff_TP, 
                      CASE [Simple_Add-On] WHEN 'S1' THEN [Total Principal] + DSLP * PerDiem ELSE RemBalance78 END + CF_E.LateCharges + CF_E.[NSF Balance] AS PayOff_Calc,
                       CF_E.PerDiem, N_L.NoteDate1, ISNULL(dbo.t_Collection.StatusID, dbo.fs_StateID__Collection__Start()) AS StatusID_Collection, 
                      ISNULL(S_Collection.Name, N'') AS Status_Collection, UN.UserName, dbo.t_Loan.GUID_Documents, CF_E.PaymentsMadeByPrinBal, CF_E.NameLast, 
                      CF_E.NameFirst, CF_E.PaymentAmountM, ISNULL(REPO.StatusID, dbo.fs_StateID__Repo__NoRepo()) AS StatusID_Repo, ISNULL(REPO.Status, N'') 
                      AS Status_Repo, REPO.StatusDate AS StatusDate_Repo, REPO.StatusUser AS StatusUser_Repo, ISNULL(REPO.AgentUserName, N'') AS RepoAgent, 
                      B0.PhoneCell, B0.PhoneOther, N_L.NoteDate0 AS NoteLastDate, ISNULL(DATEDIFF(dd, N_L.NoteDate0, GETDATE()), 10000) AS DaysSinceLastNote, 
                      CONVERT(int, RIGHT(CF_E.[Customer #], 3)) AS TeamRange, CASE WHEN dbo.t_Collection.StatusID IS NULL THEN 1 ELSE ISNULL(CONVERT(bit, 
                      DoCollect.StateParameterValue), 0) END AS DoCollect
FROM         (SELECT     dbo.UserName() AS UserName) AS UN CROSS JOIN
                      dbo.fs_StatesParamValue_DoCollect() AS DoCollect RIGHT OUTER JOIN
                      dbo.t_State AS S_Collection INNER JOIN
                      dbo.t_Collection ON S_Collection.ID = dbo.t_Collection.StatusID ON DoCollect.StateID = dbo.t_Collection.StatusID RIGHT OUTER JOIN
                      usix.t_CustomerFile_Export AS CF_E LEFT OUTER JOIN
                      dbo.v_Repossession AS REPO ON CF_E.LoanID = REPO.LoanID LEFT OUTER JOIN
                      dbo.v_Borrower_Update AS B0 INNER JOIN
                      dbo.t_Loan INNER JOIN
                      dbo.t_State AS S ON dbo.t_Loan.StatusID = S.ID INNER JOIN
                      dbo.t_Loan_Borrower AS L_B0 ON dbo.t_Loan.ID = L_B0.LoanID ON B0.ID = L_B0.BorrowerID ON dbo.t_Loan.ID = CF_E.LoanID ON 
                      dbo.t_Collection.LoanID = CF_E.LoanID LEFT OUTER JOIN
                      dbo.ts_State INNER JOIN
                      dbo.ts_TimeZone ON dbo.ts_State.TimeZone = dbo.ts_TimeZone.TimeZone AND dbo.ts_State.TimeZone = dbo.ts_TimeZone.TimeZone AND 
                      dbo.ts_State.TimeZone = dbo.ts_TimeZone.TimeZone ON CF_E.St = dbo.ts_State.StateCode LEFT OUTER JOIN
                      dbo.v_Note_PTP_Open AS N_PTP_O ON CF_E.LoanID = N_PTP_O.LoanID LEFT OUTER JOIN
                      dbo.v_Note_Last AS N_L ON CF_E.LoanID = N_L.LoanID

GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane1', @value = N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4[30] 2[40] 3) )"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4[50] 3) )"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[49] 4[25] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1[39] 4) )"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4) )"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "t_Loan"
            Begin Extent = 
               Top = 234
               Left = 93
               Bottom = 342
               Right = 273
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "S"
            Begin Extent = 
               Top = 6
               Left = 450
               Bottom = 114
               Right = 601
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "CF_E"
            Begin Extent = 
               Top = 6
               Left = 639
               Bottom = 114
               Right = 838
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "N_L"
            Begin Extent = 
               Top = 6
               Left = 876
               Bottom = 84
               Right = 1036
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "REPO"
            Begin Extent = 
               Top = 114
               Left = 227
               Bottom = 222
               Right = 385
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "B0"
            Begin Extent = 
               Top = 546
               Left = 82
               Bottom = 654
               Right = 304
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "L_B0"
            Begin Extent = 
               Top = 436
               Left = 243
               Bottom = 544
               Right = 394
            End
            DisplayFlags = 280
            TopColumn = 0', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_Test';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane2', @value = N'End
         Begin Table = "S_Collection"
            Begin Extent = 
               Top = 150
               Left = 928
               Bottom = 258
               Right = 1079
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "t_Collection"
            Begin Extent = 
               Top = 212
               Left = 673
               Bottom = 320
               Right = 824
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "ts_State"
            Begin Extent = 
               Top = 356
               Left = 397
               Bottom = 449
               Right = 548
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "ts_TimeZone"
            Begin Extent = 
               Top = 646
               Left = 322
               Bottom = 739
               Right = 473
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "N_PTP_O"
            Begin Extent = 
               Top = 492
               Left = 468
               Bottom = 600
               Right = 619
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "UN"
            Begin Extent = 
               Top = 521
               Left = 717
               Bottom = 584
               Right = 868
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "DoCollect"
            Begin Extent = 
               Top = 332
               Left = 880
               Bottom = 410
               Right = 1062
            End
            DisplayFlags = 280
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 159
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_Test';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPane3', @value = N'Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 7515
         Alias = 1725
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_Test';


GO
EXECUTE sp_addextendedproperty @name = N'MS_DiagramPaneCount', @value = 3, @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'VIEW', @level1name = N'v_Test';

